package com.axend.aerosense.room.ui.provider;

import android.util.ArraySet;

import com.axend.aerosense.base.bean.BaseBean;
import com.axend.aerosense.common.bean.RadarType;
import com.axend.aerosense.common.bean.FunctionRoomEnum;
import com.axend.aerosense.room.entity.RoomTypeEnum;
import com.blankj.utilcode.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;

public class VipFunctionRepository {

    //房间与设备的关系
    private static Map<RoomTypeEnum, RoomDeviceMapping> roomDeviceMap;
    //设备与功能的关系
    private static Map<RadarType, DeviceFunctionMapping> deviceFunctionMap;
    //房间与功能的关系
    private static Map<RoomTypeEnum, RoomTypeFunctionMapping> roomTypeFunctionMap;

    static {
        initRoomDeviceMap();
        initDeviceFunctionMap();
        initRoomFunctionMap();
    }

    /**
     * 房间与设备关系
     */
    private static void initRoomDeviceMap(){
        if (null == roomDeviceMap) {
            roomDeviceMap = new HashMap<>();
        }

        //客厅
        roomDeviceMap.put(
                RoomTypeEnum.LIVING_ROOM,
                new RoomDeviceMapping(
                        RoomTypeEnum.LIVING_ROOM,
                        Arrays.asList(
                                RadarType.ASSURE,
                                RadarType.ASSURE_LITE
                        )
                )
        );

        //卧室
        roomDeviceMap.put(
                RoomTypeEnum.BEDROOM,
                new RoomDeviceMapping(
                        RoomTypeEnum.BEDROOM,
                        Arrays.asList(
                                RadarType.ASSURE,
                                RadarType.ASSURE_LITE,
                                RadarType.WAVVE,
                                RadarType.WAVVE_PRO
                        )
                )
        );

        //卫浴
        roomDeviceMap.put(
                RoomTypeEnum.BATHROOM,
                new RoomDeviceMapping(
                        RoomTypeEnum.BATHROOM,
                        Arrays.asList(
                                RadarType.ASSURE,
                                RadarType.ASSURE_LITE
                        )
                )
        );

        //厨房
        roomDeviceMap.put(
                RoomTypeEnum.KITCHEN,
                new RoomDeviceMapping(
                        RoomTypeEnum.KITCHEN,
                        Arrays.asList(
                                RadarType.ASSURE,
                                RadarType.ASSURE_LITE
                        )
                )
        );

        //书房
        roomDeviceMap.put(
                RoomTypeEnum.OFFICE,
                new RoomDeviceMapping(
                        RoomTypeEnum.OFFICE,
                        Arrays.asList(
                                RadarType.ASSURE,
                                RadarType.ASSURE_LITE
                        )
                )
        );

        //其他
        roomDeviceMap.put(
                RoomTypeEnum.OTHERS,
                new RoomDeviceMapping(
                        RoomTypeEnum.OTHERS,
                        Arrays.asList(
                                RadarType.ASSURE,
                                RadarType.ASSURE_LITE
                        )
                )
        );
    }

    /**
     * 设备与设置功能关系
     */
    private static void initDeviceFunctionMap(){
        if (null == deviceFunctionMap) {
            deviceFunctionMap = new HashMap<>();
        }

        //ASSURE
        deviceFunctionMap.put(
                RadarType.ASSURE,
                new DeviceFunctionMapping(
                        RadarType.ASSURE,
                        Arrays.asList(
                                FunctionRoomEnum.SURVEILLANCE_MODE,
                                FunctionRoomEnum.FALL_ALERT,
                                FunctionRoomEnum.OVERSLEEPING_ALERT,
                                FunctionRoomEnum.FREQUENT_NIGHT_ACTIVITY,
                                FunctionRoomEnum.WANDERING_ALERT_NIGHT,
                                FunctionRoomEnum.WANDERING_ALERT_DAY,
                                FunctionRoomEnum.SEDENTARY_NOTIFICATION,
                                FunctionRoomEnum.BEDTIME_NOTIFICATION,
                                FunctionRoomEnum.MOTIONLESS_ALERT,
                                FunctionRoomEnum.NUTRITION_ALERT,
                                FunctionRoomEnum.EXTENDED_TOILETING_ALERT,
                                FunctionRoomEnum.FREQUENT_TOILETING_AT_NIGHT,
                                FunctionRoomEnum.MOBILITY_ANALYSIS
                        )
                )
        );

        //ASSURE LITE
        deviceFunctionMap.put(
                RadarType.ASSURE_LITE,
                new DeviceFunctionMapping(
                        RadarType.ASSURE_LITE,
                        Arrays.asList(
                                FunctionRoomEnum.SURVEILLANCE_MODE,
                                FunctionRoomEnum.FALL_ALERT,
                                FunctionRoomEnum.EXTENDED_TOILETING_ALERT,
                                FunctionRoomEnum.FREQUENT_TOILETING_AT_NIGHT
                        )
                )
        );

        //WAVVE
        deviceFunctionMap.put(
                RadarType.WAVVE,
                new DeviceFunctionMapping(
                        RadarType.WAVVE,
                        Arrays.asList(
                                FunctionRoomEnum.OVERSLEEPING_ALERT,
                                FunctionRoomEnum.FREQUENT_NIGHT_ACTIVITY,
                                FunctionRoomEnum.WANDERING_ALERT_NIGHT,
                                FunctionRoomEnum.NO_VITAL_SIGNALS,
                                FunctionRoomEnum.BED_EXIT_WARNING,
                                FunctionRoomEnum.ABNORMAL_SLEEP
                        )
                )
        );

        //WAVVE PRO
        deviceFunctionMap.put(
                RadarType.WAVVE_PRO,
                new DeviceFunctionMapping(
                        RadarType.WAVVE_PRO,
                        Arrays.asList(
                                FunctionRoomEnum.SURVEILLANCE_MODE,
                                FunctionRoomEnum.FALL_ALERT,
                                FunctionRoomEnum.OVERSLEEPING_ALERT,
                                FunctionRoomEnum.FREQUENT_NIGHT_ACTIVITY,
                                FunctionRoomEnum.WANDERING_ALERT_NIGHT,
                                FunctionRoomEnum.NO_VITAL_SIGNALS,
                                FunctionRoomEnum.BED_EXIT_WARNING,
                                FunctionRoomEnum.ABNORMAL_SLEEP,
                                FunctionRoomEnum.SIT_UP_ALERT
                        )
                )
        );
    }

    /**
     * 房间与功能关系
     */
    private static void initRoomFunctionMap(){
        if (null == roomTypeFunctionMap) {
            roomTypeFunctionMap = new HashMap<>();
        }

        //客厅
        roomTypeFunctionMap.put(
                RoomTypeEnum.LIVING_ROOM,
                new RoomTypeFunctionMapping(
                        RoomTypeEnum.LIVING_ROOM,
                        Arrays.asList(
                                FunctionRoomEnum.FALL_ALERT,
                                FunctionRoomEnum.SURVEILLANCE_MODE,
                                FunctionRoomEnum.WANDERING_ALERT_DAY,
                                FunctionRoomEnum.SEDENTARY_NOTIFICATION,
                                FunctionRoomEnum.MOTIONLESS_ALERT,
                                FunctionRoomEnum.MOBILITY_ANALYSIS
                        )
                )
        );

        //卧室
        roomTypeFunctionMap.put(
                RoomTypeEnum.BEDROOM,
                new RoomTypeFunctionMapping(
                        RoomTypeEnum.BEDROOM,
                        Arrays.asList(
                                FunctionRoomEnum.FALL_ALERT,
                                FunctionRoomEnum.SURVEILLANCE_MODE,
                                FunctionRoomEnum.OVERSLEEPING_ALERT,
                                FunctionRoomEnum.FREQUENT_NIGHT_ACTIVITY,
                                FunctionRoomEnum.WANDERING_ALERT_NIGHT,
                                FunctionRoomEnum.WANDERING_ALERT_DAY,
                                FunctionRoomEnum.SEDENTARY_NOTIFICATION,
                                FunctionRoomEnum.BEDTIME_NOTIFICATION,
                                FunctionRoomEnum.MOTIONLESS_ALERT,
                                FunctionRoomEnum.NO_VITAL_SIGNALS,
                                FunctionRoomEnum.BED_EXIT_WARNING,
                                FunctionRoomEnum.ABNORMAL_SLEEP,
                                FunctionRoomEnum.MOBILITY_ANALYSIS,
                                FunctionRoomEnum.SIT_UP_ALERT
                        )
                )
        );

        //卫浴
        roomTypeFunctionMap.put(
                RoomTypeEnum.BATHROOM,
                new RoomTypeFunctionMapping(
                        RoomTypeEnum.BATHROOM,
                        Arrays.asList(
                                FunctionRoomEnum.FALL_ALERT,
                                FunctionRoomEnum.SURVEILLANCE_MODE,
                                FunctionRoomEnum.WANDERING_ALERT_DAY,
                                FunctionRoomEnum.MOTIONLESS_ALERT,
                                FunctionRoomEnum.EXTENDED_TOILETING_ALERT,
                                FunctionRoomEnum.FREQUENT_TOILETING_AT_NIGHT
                        )
                )
        );

        //厨房
        roomTypeFunctionMap.put(
                RoomTypeEnum.KITCHEN,
                new RoomTypeFunctionMapping(
                        RoomTypeEnum.KITCHEN,
                        Arrays.asList(
                                FunctionRoomEnum.FALL_ALERT,
                                FunctionRoomEnum.SURVEILLANCE_MODE,
                                FunctionRoomEnum.SEDENTARY_NOTIFICATION,
                                FunctionRoomEnum.NUTRITION_ALERT,
                                FunctionRoomEnum.MOBILITY_ANALYSIS
                        )
                )
        );

        //书房
        roomTypeFunctionMap.put(
                RoomTypeEnum.OFFICE,
                new RoomTypeFunctionMapping(
                        RoomTypeEnum.OFFICE,
                        Arrays.asList(
                                FunctionRoomEnum.FALL_ALERT,
                                FunctionRoomEnum.SURVEILLANCE_MODE,
                                FunctionRoomEnum.SEDENTARY_NOTIFICATION,
                                FunctionRoomEnum.MOTIONLESS_ALERT,
                                FunctionRoomEnum.MOBILITY_ANALYSIS
                        )
                )
        );

        //其他
        roomTypeFunctionMap.put(
                RoomTypeEnum.OTHERS,
                new RoomTypeFunctionMapping(
                        RoomTypeEnum.OTHERS,
                        Arrays.asList(
                                FunctionRoomEnum.FALL_ALERT,
                                FunctionRoomEnum.SURVEILLANCE_MODE,
                                FunctionRoomEnum.MOTIONLESS_ALERT,
                                FunctionRoomEnum.MOBILITY_ANALYSIS
                        )
                )
        );
    }

    /** 获取房间对应的设备类型列表 */
    public static RoomDeviceMapping getRoomDeviceMapping(RoomTypeEnum roomTypeEnum){
        if (null == roomTypeEnum) {
            throw new IllegalArgumentException("roomTypeEnum is null");
        }

        return roomDeviceMap.get(roomTypeEnum);
    }

    /** 获取雷达设备对应的设置功能 */
    public static Set<FunctionRoomEnum> getDeviceFunctionMapping(List<RadarType> radarTypes){
        Set<FunctionRoomEnum> functionList = new ArraySet<>();
        for (RadarType type : radarTypes) {
            functionList.addAll(getDeviceFunctionMapping(type).functionList);
        }
        return functionList;
    }

    /** 获取雷达设备对应的设置功能 */
    public static DeviceFunctionMapping getDeviceFunctionMapping(RadarType radarType){
        if (null == radarType) {
            throw new IllegalArgumentException("RadarType is null");
        }

        return deviceFunctionMap.get(radarType);
    }

    /** 获取房间对应的设置功能 */
    public static RoomTypeFunctionMapping getRoomTypeFunctionMapping(RoomTypeEnum roomTypeEnum){
        if (null == roomTypeEnum) {
            throw new IllegalArgumentException("roomTypeEnum is null");
        }

        return roomTypeFunctionMap.get(roomTypeEnum);
    }

    /** 通过房间类型与设备类型获取对应的功能列表 */
    public static Set<FunctionRoomEnum> getRoomFunctionList(RoomTypeEnum roomTypeEnum, List<RadarType> radarTypeList){

        Set<FunctionRoomEnum> result = new ArraySet<>();

        for (RadarType radarType : radarTypeList) {
            if(checkRoomDeviceMapping(roomTypeEnum,radarType)){
                Set<FunctionRoomEnum> deviceFunctionSet = getDeviceFunctionMapping(radarTypeList);
                List<FunctionRoomEnum> roomTypeFunctionList = getRoomTypeFunctionMapping(roomTypeEnum).functionList;

                for (FunctionRoomEnum functionRoomEnum : deviceFunctionSet) {
                    if (roomTypeFunctionList.contains(functionRoomEnum)) {
                        result.add(functionRoomEnum);
                    }
                }
            }
        }

        return result;
    }

    private static boolean checkRoomDeviceMapping(RoomTypeEnum roomTypeEnum, RadarType radarType){
        RoomDeviceMapping roomDeviceMapping = roomDeviceMap.get(roomTypeEnum);
        if(ObjectUtils.isEmpty(roomDeviceMapping)){
            return false;
        }
        return roomDeviceMapping.getRadarList().contains(radarType);
    }


    @Data
    @EqualsAndHashCode(callSuper = true)
    @AllArgsConstructor
    public static class RoomDeviceMapping extends BaseBean {
        private RoomTypeEnum roomTypeEnum;  //房间类型
        private List<RadarType> radarList;  //当前房间可出现的设备类型，每个房间 同一类型 的设备只能安装一台
    }

    @Data
    @EqualsAndHashCode(callSuper = true)
    @AllArgsConstructor
    public static class DeviceFunctionMapping extends BaseBean {
        private RadarType radarType;    //雷达类型
        private List<FunctionRoomEnum> functionList;     //当前设备下可有的设置功能
    }

    @Data
    @EqualsAndHashCode(callSuper = true)
    @AllArgsConstructor
    public static class RoomTypeFunctionMapping extends BaseBean {
        private RoomTypeEnum roomTypeEnum;    //房间类型
        private List<FunctionRoomEnum> functionList;     //当前房间下可有的设置功能
    }
}
